package com.example.validatestudy.demos.dataStructures;

import lombok.Data;

import java.util.LinkedList;

/**
 * @param
 * @version v1
 * @auther WANGZHENFANG004
 * @summary
 * @since 2024 06 22 20 56
 */
public class LinkedTest {

    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.removeLast();

        /**
         * 做练习题
         */
        test();

    }

    public static void test(){

        Node node = init();
        /**
         * 链表反转
         */
        node = test1(node);
        print(node);

        node = reverseList(node);
        print(node);
    }


    /**
     * do while循环
     * @param node
     * @return
     */
    public static Node test1(Node node){
        boolean head = true;
        Node f = node;//f=1
        Node s = f.getNext();//s=2
        Node tmp;
        do{
            if(s!=null){
                if(head){
                    f.next = null;//1>null
                    head = false;
                }
                tmp = s.next;//tmp=3 4 null
                s.next = f;//2>1  3>2 4>3
                f = s;//f = 2 3 4
                s = tmp;//s=3 4 null
            }
        }while (s!=null);
        return f;
    }

    public static void print(Node node){
        Node now = node;
        do{
            if(now!=null){
                System.out.println(now.getValue());
            }
            now = now.getNext();
        }while (now!=null);
    }


    //递归方法
    public static Node reverseList(Node node){
        if(node==null || node.next==null){
            return node;
        }
        Node next = node.next;
        Node newHead = reverseList(next);
        next.next = node;
        node.next = null;
        return newHead;
    }

    public static Node init(){
        Node head = new Node(1);
        Node head1 =  new Node(2);
        Node head2 =  new Node(3);
        Node head3 =  new Node(4);
        head.setNext(head1);
        head1.setNext(head2);
        head2.setNext(head3);
        return head;
    }



    @Data
    static class Node{
        private int value;
        private Node next;
        public Node() {
        }
        public Node(int value) {
            this.value = value;
        }
        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }
}
